<template>
   <div class="fix">
        <div class='fix-item' v-for="(item, key) of citys" :key="key" :ref="key"
        @touchstart="start"
        @touchmove="move"
        @touchend="end"
        @click='outputkey'
        >{{key}}</div>
   </div>
</template>

<script>
export default {
  name: 'Fix',
  props: {
    citys: Object
  },
  data () {
    return {
      contrl: false,
      startY: null,
      time: null
    }
  },
  computed: {
    list () {
      const nba = []
      for (let i in this.citys) {
        nba.push(i)
      }
      return nba
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    outputkey (el) {
      this.$emit('change', el.target.innerText)
    },
    start () {
      this.contrl = true
    },
    move (e) {
      if (this.contrl) {
        if (this.time) {
          clearTimeout(this.time)
        }
        this.time = setTimeout(() => {
          const touchY = e.touches[0].clientY - 79
          const index = Math.floor((touchY - this.startY) / 20)
          if (index >= 0 && index < this.list.length) {
            this.$emit('change', this.list[index])
          }
        }, 20)
      }
    },
    end () {
      this.contrl = false
    }
  }
}
</script>

<style lang="stylus" scoped>
 .fix
  position: absolute
  width: .4rem
  top: 1.58rem
  right: 0
  bottom: 0
  display: flex
  flex-direction:column
  justify-content: center
  .fix-item
   line-height: .4rem
   text-align: center
   color: #00bcd4
</style>
